﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.UI.Models;
using CMS.BL.Entity;
using CMS.Utils;
using CMS.BL;
using System.Globalization;


namespace CMS.UI
{
    public class LeftMenuModelMapper
    {
        public static List<CMSMenuModel> MapDomains(List<tbl_Domains> domains)
        {
            var menuModel = new CMSMenuModel { Title = "Domains", MenuItems = new List<CMSMenuItem>() };
            foreach (var domain in domains)
            {
                menuModel.MenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = domain.DomainID,
                    Title = domain.DO_Domain,

                    IsManage = true,

                    EditText = "Edit " + domain.DO_Domain,
                    DeleteText = "Delete " + domain.DO_Domain,
                });
            }
            return new List<CMSMenuModel> { menuModel };
        }

        public static List<CMSMenuModel> MapUsers(List<tbl_AdminUsers> users, CustomPrincipal currentUser)
        {
            var menuModel = new CMSMenuModel { Title = "Users", MenuItems = new List<CMSMenuItem>() };
            foreach (var user in users)
            {
                menuModel.MenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = user.AdminUserID,
                    Title = String.Format("<b>{0}: {1}</b> ({2})", user.US_UserName, user.US_Email, user.tbl_UserGroups.UG_Type),
                    IsDelete = currentUser.HasPermission(Permission.DeleteUser),
                    IsEdit = currentUser.HasPermission(Permission.EditUser)
                });
            }
            return new List<CMSMenuModel> { menuModel };
        }

        public static List<CMSMenuModel> MapUserGroups(List<tbl_UserGroups> groups)
        {
            var menuModel = new CMSMenuModel { Title = "User Groups", MenuItems = new List<CMSMenuItem>() };
            foreach (var group in groups)
            {
                menuModel.MenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = group.UserGroupID,
                    Title = group.UG_Type
                });
            }
            return new List<CMSMenuModel> { menuModel };
        }

        public static List<CMSMenuModel> MapAdminMenu(List<tbl_AdminMenu> menuItems)
        {
            return new List<CMSMenuModel> { 
                new CMSMenuModel { 
                    Title = "Modules", 
                    MenuItems = CreateAdminSubMenu(menuItems, 0)
                } 
            };
        }

        private static List<CMSMenuItem> CreateAdminSubMenu(List<tbl_AdminMenu> menuItems, int parentID)
        {
            var subMenuItems = new List<CMSMenuItem>();
            foreach (var menuItem in menuItems.OrderBy(mi => mi.AM_Order).Where(mi => mi.AM_ParentID == parentID))
            {
                subMenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = menuItem.AdminMenuID,
                    SubMenuItems = CreateAdminSubMenu(menuItems, menuItem.AdminMenuID),
                    Title = menuItem.AM_MenuText,
                    Visible = menuItem.AM_Live,

                    IsMove = true,
                    IsVisibility = true,
                    VisibilityText = "Turn menu item on/off"
                });
            }
            return subMenuItems;
        }

        public static List<CMSMenuModel> MapSections(List<tbl_Domains> domains, CustomPrincipal currentUser)
        {
            var sectionModels = new List<CMSMenuModel>();

            foreach (var domain in domains)
            {
                var sections = domain.tbl_SiteMap.Where(sm => !sm.SM_Deleted && sm.tbl_ContentType.CTP_Value.Equals(ContentType.Content.ToString()));
                var sectionModel = new CMSMenuModel { Title = domain.DO_CompanyName, MenuItems = new List<CMSMenuItem>() };

                sectionModel.MenuItems = CreateSectionsSubMenu(sections, 0, currentUser);
                sectionModels.Add(sectionModel);
            }

            return sectionModels;
        }

        private static List<CMSMenuItem> CreateSectionsSubMenu(IEnumerable<tbl_SiteMap> sections, int parentID, CustomPrincipal currentUser)
        {
            var subMenuItems = new List<CMSMenuItem>();
            foreach (var section in sections.Where(sm => sm.SM_ParentID == parentID).OrderBy(sm => sm.SM_OrderBy))
            {
                subMenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = section.SiteMapID,
                    SubMenuItems = CreateSectionsSubMenu(sections, section.SiteMapID, currentUser),
                    Title = section.SM_Name,

                    IsMove = currentUser.HasPermission(Permission.EditContent),
                    IsApprove = section.tbl_Content.Where(c => !c.C_Deleted).All(c => !c.C_Approved) && currentUser.HasPermission(Permission.ApproveContent),
                    ApproveText = String.Format("{0} Content Requires Approval", section.SM_Name),
                    IsDelete = currentUser.HasPermission(Permission.DeleteContent),
                    DeleteText = String.Format("Delete {0} ({1})", section.SM_Name, section.SiteMapID),
                    IsEdit = currentUser.HasPermission(Permission.EditContent),
                    EditText = String.Format("Edit {0} ({1})", section.SM_Name, section.SiteMapID),
                    IsEditImages = currentUser.HasPermission(Permission.EditContent),
                    EditImagesText = String.Format("Images for {0} ({1})", section.SM_Name, section.SiteMapID),
                    IsExpand = sections.Any(c => c.SM_ParentID == section.SiteMapID),
                    ExpandText = String.Format("Show Sub Pages of {0}", section.SM_Name),
                    IsPreview = true,
                    PreviewText = String.Format("Preview {0}", section.SM_Name),
                    PreviewUrl = String.Format("/Admn/Preview?sectionID={0}", section.SiteMapID)
                });
            }
            return subMenuItems;
        }

        public static List<CMSMenuModel> MapNews(List<tbl_Domains> domains, CustomPrincipal currentUser)
        {
            var blogModels = new List<CMSMenuModel>();

            foreach (var domain in domains)
            {
                var blogs = domain.tbl_SiteMap.Where(sm => !sm.SM_Deleted && sm.tbl_ContentType.CTP_Value.Equals(ContentType.Blog.ToString())).OrderByDescending(b => b.SM_Date);
                var blogModel = new CMSMenuModel { Title = "Blog Articles for " + domain.DO_CompanyName, MenuItems = new List<CMSMenuItem>() };

                foreach (var b in blogs)
                {
                    blogModel.MenuItems.Add(new CMSMenuItem()
                    {
                        MenuItemID = b.SiteMapID,
                        Date = b.SM_Date,
                        Title = b.SM_Name,
                        IsDelete = currentUser.HasPermission(Permission.DeleteNews),
                        DeleteText = String.Format("Delete {0} ({1})", b.SM_Name, b.SiteMapID),
                        IsEdit = currentUser.HasPermission(Permission.EditNews),
                        EditText = String.Format("Edit \"{0}\"", b.SM_Name),
                        IsEditImages = currentUser.HasPermission(Permission.EditNews),
                        EditImagesText = String.Format("Edit images for \"{0}\"", b.SM_Name),
                        IsComment = true,
                        CommentsText = String.Format("View comments for \"{0}\"", b.SM_Name),
                        UnauthorizedCommentExists = b.tbl_Comments.Any(c => !c.CO_Authorised),
                        AuthorizedCommentExists = b.tbl_Comments.Any(c => c.CO_Authorised),
                        IsVisibility = true,
                        VisibilityText = "Turn news article on / off",
                        Visible = b.SM_Live
                    });
                }
                blogModels.Add(blogModel);
            }
            return blogModels;
        }

        public static List<CMSMenuModel> MapTestimonials(List<tbl_Testimonials> testimonials)
        {
            var menuModel = new CMSMenuModel { Title = "Testimonials", MenuItems = new List<CMSMenuItem>() };
            foreach (var t in testimonials)
            {
                menuModel.MenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = t.TestimonialID,
                    Date = t.TE_Date,
                    Title = String.Format("{1} ({0})", t.TE_Company, t.TE_Client),
                    IsDelete = true,
                    DeleteText = String.Format("Delete {1} ({0})", t.TE_Company, t.TE_Client),
                    IsEdit = true,
                    EditText = String.Format("Edit: {1} ({0})", t.TE_Company, t.TE_Client),
                    IsVisibility = true,
                    VisibilityText = "Turn testimonial on / off",
                    Visible = t.TE_Live
                });
            }

            return new List<CMSMenuModel> { menuModel };
        }

        public static List<CMSMenuModel> MapForms(List<tbl_Domains> domains, List<tbl_Form> forms, CustomPrincipal currentUser)
        {
            var submissionModels = new List<CMSMenuModel>();

            foreach (var domain in domains)
            {
                var domainForms = forms.Where(f => f.F_DomainID == domain.DomainID);
                var submissionModel = new CMSMenuModel { Title = "Forms for " + domain.DO_CompanyName, MenuItems = new List<CMSMenuItem>() };

                foreach (var f in domainForms)
                {
                    submissionModel.MenuItems.Add(new CMSMenuItem()
                    {
                        MenuItemID = f.FormID,
                        Title = f.F_Name,
                        IsEdit = false,
                        IsDelete = false,
                        IsComment = true,
                        CommentsText = String.Format("View form submissions for \"{0}\"", f.F_Name),
                        UnauthorizedCommentExists = f.tbl_FormSubmission.Any(fs => !fs.FS_Read),
                        AuthorizedCommentExists = f.tbl_FormSubmission.Any(fs => fs.FS_Read)
                    });
                }
                submissionModels.Add(submissionModel);
            }

            return submissionModels;
        }

        public static List<CMSMenuModel> MapFormItems(List<tbl_Domains> domains, List<tbl_Form> forms, List<tbl_FormItem> formsItems, CustomPrincipal currentUser)
        {
            var formModels = new List<CMSMenuModel>();

            foreach (var domain in domains)
            {
                var domainForms = forms.Where(f => f.F_DomainID == domain.DomainID);
                var domainModel = new CMSMenuModel { Title = "Forms for " + domain.DO_CompanyName, MenuItems = new List<CMSMenuItem>() };

                foreach (var form in domainForms.OrderBy(f => f.F_Name))
                {
                    domainModel.MenuItems.Add(new CMSMenuItem()
                    {
                        MenuItemID = form.FormID,
                        Title = form.F_Name,
                        IsDelete = true,
                        IsEdit = true,
                        SubMenuItems = CreateFormItemsSubMenu(formsItems.Where(fi => fi.FI_FormID == form.FormID).ToList(), currentUser)
                    });
                }
                formModels.Add(domainModel);
            }

            return formModels;
        }

        private static List<CMSMenuItem> CreateFormItemsSubMenu(List<tbl_FormItem> formsItems, CustomPrincipal currentUser)
        {
            var subMenuItems = new List<CMSMenuItem>();

            foreach (var item in formsItems.OrderBy(fi => fi.FI_Order))
            {
                subMenuItems.Add(new CMSMenuItem()
                {
                    MenuItemID = item.FormItemID,
                    Title = item.FI_Name,
                    IsDelete = true,
                    DeleteText = String.Format("Delete {0}", item.FI_Name),
                    IsEdit = true,
                    EditText = String.Format("Edit: {0}", item.FI_Name),
                    IsVisibility = true,
                    VisibilityText = "Turn item on / off",
                    Visible = item.FI_Live,
                    IsMove = true
                });
            }

            return subMenuItems;
        }
    }
}